package com.zlp.mall.modules.pms.controller;


import com.zlp.mall.common.api.Pager;
import com.zlp.mall.common.api.R;
import com.zlp.mall.modules.pms.dto.req.ProductAddReq;
import com.zlp.mall.modules.pms.dto.req.ProductQueryReq;
import com.zlp.mall.modules.pms.dto.req.ProductUpdReq;
import com.zlp.mall.modules.pms.dto.resp.ProductResp;
import com.zlp.mall.modules.pms.dto.resp.ProductUpdInfoResp;
import com.zlp.mall.modules.pms.service.ProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 商品信息 前端控制器
 * </p>
 *
 * @author ZouLiPing
 * @since 2021-09-23
 */
@RestController
@RequestMapping("/product")
@RequiredArgsConstructor
@Api(value = "ProductController", tags = "商品模块")
public class ProductController {

    private final ProductService productService;


    @GetMapping(value = "/list")
    @ApiOperation(value = "根据分页获取商品列表")
    public R<Pager<ProductResp>> list(ProductQueryReq productQueryReq) {

        Pager<ProductResp> page = productService.list(productQueryReq);
        return R.success(page);
    }


    @GetMapping("/detail/{id}")
    @ApiOperation(value = "获取商品详情获")
    public R getProductDetail(
            @PathVariable(value = "id") @ApiParam(name = "id", value = "ID") Long id
    ) {
        ProductResp productResp = productService.getProductDetail(id);
        return R.success(productResp);
    }

    @ApiOperation("创建商品")
    @PostMapping(value = "/create")
    public R create(@RequestBody ProductAddReq productAddReq) {

        boolean result = productService.create(productAddReq);
        return R.success(result);
    }

    @GetMapping(value = "/updateInfo/{id}")
    @ApiOperation("根据商品ID获取商品编辑信息")
    public R<ProductUpdInfoResp> getUpdateInfo(@PathVariable Long id) {

        ProductUpdInfoResp productResult = productService.getUpdateInfo(id);
        return R.success(productResult);
    }

    @ApiOperation("更新商品")
    @PostMapping(value = "/update/{id}")
    public R update(@PathVariable Long id, @RequestBody ProductUpdReq productUpdReq) {

        boolean result = productService.update(id, productUpdReq);
        if (result) {
            return R.success(result);
        }
        return R.failed();
    }


    @ApiOperation("批量修改删除状态")
    @PostMapping(value = "/update/deleteStatus")
    public R updateDeleteStatus(@RequestParam("ids") List<Long> ids,
                                @RequestParam("deleteStatus") Integer deleteStatus) {

        boolean result = productService.updateDeleteStatus(ids, deleteStatus);
        if (result) {
            return R.success(result);
        }
        return R.failed();
    }


    @GetMapping(value = "/simpleList")
    @ApiOperation("根据商品名称或货号模糊查询")
    public R<List<ProductResp>> simpleList(String keyword) {
        List<ProductResp> productList = productService.simpleList(keyword);
        return R.success(productList);
    }

    @ApiOperation("批量修改审核状态")
    @PostMapping(value = "/update/verifyStatus")
    public R updateVerifyStatus(@RequestParam("ids") List<Long> ids,
                                @RequestParam("verifyStatus") Integer verifyStatus,
                                @RequestParam("detail") String detail) {
        boolean result = productService.updateVerifyStatus(ids, verifyStatus, detail);
        if (result) {
            return R.success(result);
        }
        return R.failed();
    }

    @ApiOperation("批量上下架")
    @PostMapping(value = "/update/publishStatus")
    public R updatePublishStatus(@RequestParam("ids") List<Long> ids,
                                 @RequestParam("publishStatus") Integer publishStatus) {

        boolean result = productService.updatePublishStatus(ids, publishStatus);
        if (result) {
            return R.success(result);
        }
        return R.failed();
    }

    @ApiOperation("批量推荐商品")
    @PostMapping(value = "/update/recommendStatus")
    public R updateRecommendStatus(@RequestParam("ids") List<Long> ids,
                                   @RequestParam("recommendStatus") Integer recommendStatus) {

        boolean result = productService.updateRecommendStatus(ids, recommendStatus);
        if (result) {
            return R.success(result);
        }
        return R.failed();
    }

    @ApiOperation("批量设为新品")
    @PostMapping(value = "/update/newStatus")
    public R updateNewStatus(@RequestParam("ids") List<Long> ids,
                             @RequestParam("newStatus") Integer newStatus) {

        boolean result = productService.updateNewStatus(ids, newStatus);
        if (result) {
            return R.success(result);
        }
        return R.failed();
    }

}

